package dialogos;

import java.awt.Color;
import java.awt.event.KeyEvent;
import net.java.balloontip.BalloonTip;
import net.java.balloontip.styles.MinimalBalloonStyle;
import net.java.balloontip.utils.TimingUtils;
import utilidades.LeerArchivo;


/**
 *
 * @author Shidobu
 */
public class DialogoAutenticacion extends javax.swing.JDialog {

	String nombre;

	public String getNombre () {
		return nombre;
	}

	public void setNombre (String nombre) {
		this.nombre = nombre;
	}

	/** Creates new form DialogoAutenticacion */
	public DialogoAutenticacion (java.awt.Frame parent, boolean modal) {
		super (parent, modal);
		initComponents ();
		this.setLocationRelativeTo (null);
	}

	public void autenticar () {
		LeerArchivo usuario = new LeerArchivo ();
		try {
			if (usuario.buscar (jTextFieldNombreUsuario.getText (), String.valueOf (jPasswordFieldContraseña.getPassword ()))) {
				this.setNombre (jTextFieldNombreUsuario.getText ());
				this.setVisible (false);
			} else {
				TimingUtils.showTimedBalloon (new BalloonTip (jButtonIngresar, "No existe el usuario o la contraseña es incorrecta",
					new MinimalBalloonStyle (new Color (231, 225, 165), 2), BalloonTip.Orientation.RIGHT_BELOW,
					BalloonTip.AttachLocation.SOUTH, 20, 10, false), 3000);
			}
		} catch (Exception e) {
			System.out.println ("ERROR AL CARGAR DATOS DE ARCHIVO");
		}
	}

	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	@SuppressWarnings ("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabelLogo = new javax.swing.JLabel();
        jPanelIdentificacion = new javax.swing.JPanel();
        jLabelNombreUsuario = new javax.swing.JLabel();
        jLabelContraseña = new javax.swing.JLabel();
        jTextFieldNombreUsuario = new javax.swing.JTextField();
        jButtonIngresar = new javax.swing.JButton();
        jPasswordFieldContraseña = new javax.swing.JPasswordField();
        jLabelBienvenida = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("REUSA - Autenticación");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        jLabelLogo.setForeground(new java.awt.Color(51, 204, 0));
        jLabelLogo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/reusaok.png"))); // NOI18N
        jLabelLogo.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));

        jPanelIdentificacion.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Identificación", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 11), new java.awt.Color(0, 153, 0))); // NOI18N

        jLabelNombreUsuario.setText("Nombre de usuario:");

        jLabelContraseña.setText("Contraseña:");

        jTextFieldNombreUsuario.setToolTipText("Ingrese su nombre de usuario");
        jTextFieldNombreUsuario.setMaximumSize(new java.awt.Dimension(150, 20));
        jTextFieldNombreUsuario.setMinimumSize(new java.awt.Dimension(150, 20));
        jTextFieldNombreUsuario.setPreferredSize(new java.awt.Dimension(150, 20));
        jTextFieldNombreUsuario.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                presionaEnter(evt);
            }
        });

        jButtonIngresar.setText("Ingresar");
        jButtonIngresar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonIngresarActionPerformed(evt);
            }
        });

        jPasswordFieldContraseña.setToolTipText("Ingrese su contraseña");
        jPasswordFieldContraseña.setMaximumSize(new java.awt.Dimension(150, 20));
        jPasswordFieldContraseña.setMinimumSize(new java.awt.Dimension(150, 20));
        jPasswordFieldContraseña.setPreferredSize(new java.awt.Dimension(150, 20));
        jPasswordFieldContraseña.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                presionaEnter(evt);
            }
        });

        javax.swing.GroupLayout jPanelIdentificacionLayout = new javax.swing.GroupLayout(jPanelIdentificacion);
        jPanelIdentificacion.setLayout(jPanelIdentificacionLayout);
        jPanelIdentificacionLayout.setHorizontalGroup(
            jPanelIdentificacionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelIdentificacionLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelIdentificacionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabelNombreUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabelContraseña, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanelIdentificacionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jPasswordFieldContraseña, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 150, Short.MAX_VALUE)
                    .addComponent(jTextFieldNombreUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanelIdentificacionLayout.createSequentialGroup()
                .addContainerGap(191, Short.MAX_VALUE)
                .addComponent(jButtonIngresar)
                .addContainerGap())
        );
        jPanelIdentificacionLayout.setVerticalGroup(
            jPanelIdentificacionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelIdentificacionLayout.createSequentialGroup()
                .addGroup(jPanelIdentificacionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelNombreUsuario)
                    .addComponent(jTextFieldNombreUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanelIdentificacionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelContraseña)
                    .addComponent(jPasswordFieldContraseña, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jButtonIngresar)
                .addContainerGap())
        );

        jLabelBienvenida.setFont(new java.awt.Font("Tahoma", 1, 18));
        jLabelBienvenida.setText("\"Bienvenido al Sistema de Cotización\"");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(30, 30, 30)
                        .addComponent(jLabelBienvenida))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabelLogo, javax.swing.GroupLayout.PREFERRED_SIZE, 386, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(50, 50, 50)
                        .addComponent(jPanelIdentificacion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabelBienvenida, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabelLogo)
                .addGap(28, 28, 28)
                .addComponent(jPanelIdentificacion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(34, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

	private void jButtonIngresarActionPerformed (java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonIngresarActionPerformed
		if (jTextFieldNombreUsuario.getText ().equals ("")) {
			TimingUtils.showTimedBalloon (new BalloonTip (jTextFieldNombreUsuario, "Debe ingresar el nombre de usuario",
				new MinimalBalloonStyle (new Color (231, 225, 165), 2), BalloonTip.Orientation.RIGHT_ABOVE,
				BalloonTip.AttachLocation.ALIGNED, 20, 10, false), 3000);
		} else {
			if (String.valueOf (jPasswordFieldContraseña.getPassword ()).equals ("")) {
				TimingUtils.showTimedBalloon (new BalloonTip (jPasswordFieldContraseña, "Debe ingresar la contraseña",
					new MinimalBalloonStyle (new Color (231, 225, 165), 2), BalloonTip.Orientation.RIGHT_ABOVE,
					BalloonTip.AttachLocation.ALIGNED, 20, 10, false), 3000);
			} else {
				autenticar ();
			}
		}
	}//GEN-LAST:event_jButtonIngresarActionPerformed

	private void formWindowClosing (java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
		System.exit (0);
	}//GEN-LAST:event_formWindowClosing

	private void presionaEnter (java.awt.event.KeyEvent evt) {//GEN-FIRST:event_presionaEnter

		if (evt.getKeyChar () == KeyEvent.VK_ENTER) {
			if (jTextFieldNombreUsuario.getText ().equals ("")) {
				TimingUtils.showTimedBalloon (new BalloonTip (jTextFieldNombreUsuario, "Debe ingresar el nombre de usuario",
					new MinimalBalloonStyle (new Color (231, 225, 165), 2), BalloonTip.Orientation.RIGHT_ABOVE,
					BalloonTip.AttachLocation.ALIGNED, 20, 10, false), 3000);
			} else {
				if (String.valueOf (jPasswordFieldContraseña.getPassword ()).equals ("")) {
					TimingUtils.showTimedBalloon (new BalloonTip (jPasswordFieldContraseña, "Debe ingresar la contraseña",
						new MinimalBalloonStyle (new Color (231, 225, 165), 2), BalloonTip.Orientation.RIGHT_ABOVE,
						BalloonTip.AttachLocation.ALIGNED, 20, 10, false), 3000);
				} else {
					autenticar ();
				}
			}


		}

	}//GEN-LAST:event_presionaEnter

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonIngresar;
    private javax.swing.JLabel jLabelBienvenida;
    private javax.swing.JLabel jLabelContraseña;
    private javax.swing.JLabel jLabelLogo;
    private javax.swing.JLabel jLabelNombreUsuario;
    private javax.swing.JPanel jPanelIdentificacion;
    private javax.swing.JPasswordField jPasswordFieldContraseña;
    private javax.swing.JTextField jTextFieldNombreUsuario;
    // End of variables declaration//GEN-END:variables
}
